home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-06-13 | 4.7 KB | 167 lines | [TEXT/KAHL] |
- // This may look like C code, but it is really -*- C++ -*-
- /*
- ************************************************************************
- * Macintosh Service C++ functions
- * that support the standard environment for me on Macintosh
- *
- * $Id: myenv.cc,v 1.3 1995/02/08 17:23:50 oleg Exp oleg $
- */
-
-
- #include "mymenv.h"
- #include <stdarg.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <Sound.h>
- #include <new.h>
-
-
-
- static const int System_Alert_rsc = -16411; // ID of the System ALRT
- // resource (in the System file)
- /*
- *------------------------------------------------------------------------
- * Something got screwed up and we're out of here
- * Synopsis
- * volatile void _die(const char * message,... );
- * Message may contain format control sequences %x. Items to print
- * with the control sequences are to be passed as additional arguments to
- * the function call.
- */
-
- volatile void _die(const char * message,...)
- {
- va_list args;
- char buffer[1000];
- va_start(args,message); // Init 'args' to the beginning of
- // the variable length list of args
- vsprintf(buffer,message,args);
- strncat(buffer,"\rSorry man, enough is enough, I'm outta here",sizeof(buffer)-strlen(buffer)-1);
- CtoPstr(buffer); // Convert to Pascal-like string
- ParamText((ConstStr255Param)buffer,nil,nil,nil); // Set the string in the Alert resource
- Alert(System_Alert_rsc,0L); // Display the Alert box and the string
- // set just before
- exit(4);
- }
-
- // This guy handles the situation when the memory
- // allocation through 'new' has failed
- static void my_new_failed_handler(void)
- {
- _die("Flat out of memory");
- }
-
- /*
- *------------------------------------------------------------------------
- * Just tell the user something
- * Synopsis
- * void alert(const char * text,... );
- * Message may contain format control sequences %x. Items to print
- * with the control sequences are to be passed as additional arguments to
- * the function call.
- */
-
- void alert(const char * text,...)
- {
- va_list args;
- char buffer[1000];
- va_start(args,text); // Init 'args' to the beginning of
- // the variable length list of args
- vsprintf(buffer,text,args);
- CtoPstr(buffer); // Convert to Pascal-like string
- ParamText((ConstStr255Param)buffer,nil,nil,nil); // Set the string in the Alert resource
- Alert(System_Alert_rsc,0L); // Display the Alert box and the string
- }
-
- //------------------------------------------------------------------------
- // Patches to the standard environment
-
- #ifndef _myenv_h
- // Like strncpy(), but ALWAYS terminates
- // the destination string
- char * xstrncpy(char * dest, const char * src, const int len)
- {
- strncpy(dest,src,len);
- dest[len] = '\0';
- return dest;
- }
-
- #endif
-
- // Used to "implicitly" convert from a C to Pascal
- // string
- Pstr::Pstr(const char * c_str)
- {
- strncpy((char *)pas_string,c_str,sizeof(*this)-2);
- CtoPstr((char *)pas_string);
- }
-
- //------------------------------------------------------------------------
- // Sound playing on errors
-
-
- static Handle error_sound_handle = nil;
-
- static pascal void error_sound_player(short sound_no)
- {
- if( error_sound_handle != nil )
- SndPlay(nil,(SndListHandle)error_sound_handle,FALSE);
- else
- SysBeep(10);
- error_sound_handle = nil;
- }
-
- void set_error_sound(Str255 sound_name)
- {
- error_sound_handle = GetNamedResource('snd ',sound_name);
- if( error_sound_handle != nil )
- ErrorSound(NewSoundProc(error_sound_player));
- }
-
-
- /*
- *------------------------------------------------------------------------
- * Suspending the application for a specified period of time
- *
- * Unlike Delay(), the present function is generous in that in relinquishes the
- * CPU control for a specified time and thus lets other (backgrounded) application
- * run.
- * The generous waiting is implemented as waiting for a null event which is to
- * be sent to the application after the specifed timed interval expired. Note
- * the application can get woken up before the time comes if other application
- * calls WakeUpProcess().
- */
-
- void sleep(const int nticks)
- {
- EventRecord event;
- const int event_mask = 0; // Wait for null events only
- WaitNextEvent(event_mask,&event,nticks,nil);
- }
-
-
- /*
- *------------------------------------------------------------------------
- * Initialize the Macintosh
- * Initialize all the managers & memory
- */
-
- void Initialize_MAC(void)
- {
- MaxApplZone();
-
- InitGraf(&qd.thePort);
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0);
- InitCursor();
-
- set_new_handler(my_new_failed_handler);
- }
-
-
-